import edu.ads.bookloan.Author
import edu.ads.bookloan.Authority
import edu.ads.bookloan.Book
import edu.ads.bookloan.Degree
import edu.ads.bookloan.InventoryItem
import edu.ads.bookloan.InventoryItemStatus
import edu.ads.bookloan.LoanHistory
import edu.ads.bookloan.Person
import edu.ads.bookloan.PersonAuthority
import edu.ads.bookloan.Publisher
import edu.ads.bookloan.Requestmap


class BootStrap {
	def springSecurityService
	def init = { servletContext ->
		//new Requestmap(url: '/timeline', configAttribute: 'ROLE_USER').save()
		//new Requestmap(url: '/person/*', configAttribute: 'IS_AUTHENTICATED_REMEMBERED').save()
		//new Requestmap(url: '/post/followAjax', configAttribute: 'ROLE_USER').save()
		//new Requestmap(url: '/post/addPostAjax', configAttribute: 'ROLE_USER,IS_AUTHENTICATED_FULLY').save()
		Requestmap.findOrSaveWhere(url: '/**', configAttribute: 'IS_AUTHENTICATED_ANONYMOUSLY')
		Requestmap.findOrSaveWhere(url: '/user/**', configAttribute: 'ROLE_ADMIN')
		Requestmap.findOrSaveWhere(url: '/role/**', configAttribute: 'ROLE_ADMIN')
		Requestmap.findOrSaveWhere(url: '/requestmap/**', configAttribute: 'ROLE_ADMIN')
		Requestmap.findOrSaveWhere(url: '/loanHistory/**', configAttribute: 'ROLE_ADMIN,ROLE_EMPLOYEE,ROLE_STUDENT')
		Requestmap.findOrSaveWhere(url: '/inventoryItem/**', configAttribute: 'ROLE_ADMIN,ROLE_EMPLOYEE')
		Requestmap.findOrSaveWhere(url: '/degree/**', configAttribute: 'ROLE_ADMIN')
		Requestmap.findOrSaveWhere(url: '/publisher/**', configAttribute: 'ROLE_ADMIN,ROLE_EMPLOYEE')
		Requestmap.findOrSaveWhere(url: '/book/**', configAttribute: 'ROLE_ADMIN,ROLE_EMPLOYEE')
		Requestmap.findOrSaveWhere(url: '/author/**', configAttribute: 'ROLE_ADMIN')
		Person admin = Person.findOrSaveWhere(username: 'admin', password: springSecurityService.encodePassword('admin'))
		Authority adminRole = Authority.findOrSaveWhere(authority: 'ROLE_ADMIN')
		Authority studentRole = Authority.findOrSaveWhere(authority: 'ROLE_STUDENT')
		Authority employeeRole = Authority.findOrSaveWhere(authority: 'ROLE_EMPLOYEE')
		
		
		
		Degree tic = Degree.findOrSaveWhere(name: 'TIC')
		Degree economia = Degree.findOrSaveWhere(name: 'Economy')
		Degree psicologia = Degree.findOrSaveWhere(name: 'Psychology')
		
		Person student1 = Person.findOrSaveWhere(username: 'rtorres', password: springSecurityService.encodePassword('rtorres'),degree: tic)
		Person student2 = Person.findOrSaveWhere(username: 'wgarcia', password: springSecurityService.encodePassword('wgarcia'),degree: economia)
		Person student3 = Person.findOrSaveWhere(username: 'dguzman', password: springSecurityService.encodePassword('dguzman'),degree: psicologia)
		Person student4 = Person.findOrSaveWhere(username: 'ghidalgo', password: springSecurityService.encodePassword('ghidalgo'),degree: economia)
		Person employee = Person.findOrSaveWhere(username: 'employee', password: springSecurityService.encodePassword('employee'))
		InventoryItemStatus available = InventoryItemStatus.findOrSaveWhere(status: 'Available')
		InventoryItemStatus rented = InventoryItemStatus.findOrSaveWhere(status: 'Rented')
		PersonAuthority.create(admin,adminRole)
		PersonAuthority.create(student1,studentRole)
		PersonAuthority.create(student2,studentRole)
		PersonAuthority.create(student3,studentRole)
		PersonAuthority.create(student4,studentRole)
		PersonAuthority.create(employee,employeeRole)
		
		Author autor1 = Author.findOrSaveWhere(fullName: 'Ricardo Torres')
		Author autor2 =  Author.findOrSaveWhere(fullName: 'Wilson Cabral')
		Author autor3 =  Author.findOrSaveWhere(fullName: 'Diego Guzman')
		Author autor4 =  Author.findOrSaveWhere(fullName: 'Gregory Hidalgo')
		
		Publisher santillana = Publisher.findOrSaveWhere(name: 'Santillana')
		Publisher unibe = Publisher.findOrSaveWhere(name: 'UNIBE')
		Publisher susaeta = Publisher.findOrSaveWhere(name: 'Susaeta')
		Publisher oreilly = Publisher.findOrSaveWhere(name: 'OReilly')
		
		Book fisica1 = Book.findOrSaveWhere(title: "PHYSICS I", publisher: oreilly,yearOfEdition: 2011)
		Book fisica2 =Book.findOrSaveWhere(title: "PHYSICS II", publisher: santillana,yearOfEdition: 2011)
		Book fisica3 =Book.findOrSaveWhere(title: "PHYSICS III", publisher: oreilly,yearOfEdition: 2012)
		Book ads =Book.findOrSaveWhere(title: "SOFTWARE ARQUITECTURE", publisher: oreilly,yearOfEdition: 2012)
		Book md =Book.findOrSaveWhere(title: "DISCRETE MATHEMATICS", publisher: santillana,yearOfEdition: 2012)
		Book hu =Book.findOrSaveWhere(title: "UNIVERSAL HISTORY", publisher: susaeta,yearOfEdition: 2012)
		Book e2=Book.findOrSaveWhere(title: "ELECTRICITY I", publisher: susaeta,yearOfEdition: 2012)
		Book p1=Book.findOrSaveWhere(title: "PROGRAMMING I", publisher: oreilly,yearOfEdition: 2012)
		Book p2=Book.findOrSaveWhere(title: "PROGRAMMING II", publisher: santillana,yearOfEdition: 2012)
		Book p3=Book.findOrSaveWhere(title: "PROGRAMMING III", publisher: oreilly,yearOfEdition: 2012)
		Book iap=Book.findOrSaveWhere(title: "INTRODUCTION TO PROGRAMMING", publisher: unibe,yearOfEdition: 2012)
		Book l1=Book.findOrSaveWhere(title: "LEADERSHIP I", publisher: oreilly,yearOfEdition: 2012)
		
		autor1.addToBooks(fisica1)
		autor1.addToBooks(fisica2)
		autor1.addToBooks(fisica3)		
		autor1.addToBooks(e2)
		autor2.addToBooks(ads)
		autor2.addToBooks(md)
		autor2.addToBooks(hu)
		autor3.addToBooks(e2)
		autor3.addToBooks(p1)
		autor3.addToBooks(p2)
		autor4.addToBooks(iap)
		autor4.addToBooks(l1)
		autor4.addToBooks(fisica1)
		autor4.addToBooks(fisica3)
		
		
		fisica1.addToDegrees(tic)
		fisica1.addToDegrees(economia)
		fisica1.addToDegrees(psicologia)
		
		fisica2.addToDegrees(tic)
		
		fisica3.addToDegrees(tic)
		
		ads.addToDegrees(tic)
		md.addToDegrees(tic)
		
		
		hu.addToDegrees(tic)
		hu.addToDegrees(economia)
		hu.addToDegrees(psicologia)
		
		e2.addToDegrees(tic)
		
		p1.addToDegrees(tic)
		p2.addToDegrees(tic)
		p3.addToDegrees(tic)
		
		iap.addToDegrees(tic)
		
		
		l1.addToDegrees(tic)
		l1.addToDegrees(economia)
		l1.addToDegrees(psicologia)
		
		InventoryItem ifisica1 = InventoryItem.findOrSaveWhere(book: fisica1,inventoryItemStatus: rented)
		InventoryItem ifisica2 = InventoryItem.findOrSaveWhere(book: fisica2,inventoryItemStatus: rented)
		InventoryItem ifisica3 = InventoryItem.findOrSaveWhere(book: fisica3,inventoryItemStatus: rented)
		InventoryItem iprog1 = InventoryItem.findOrSaveWhere(book: p1,inventoryItemStatus: available)
		InventoryItem iprog2= InventoryItem.findOrSaveWhere(book: p2,inventoryItemStatus: rented)
		InventoryItem iads = InventoryItem.findOrSaveWhere(book: p2,inventoryItemStatus: rented)
		InventoryItem iprog3= InventoryItem.findOrSaveWhere(book: p3,inventoryItemStatus: available)
		
		LoanHistory.findOrSaveWhere(inventoryItem: ifisica1,person: student1,rentalDays: 7)
		LoanHistory.findOrSaveWhere(inventoryItem: ifisica2,person: student2,rentalDays: 0)
		LoanHistory.findOrSaveWhere(inventoryItem: ifisica3,person: student3,rentalDays: 0)
		LoanHistory.findOrSaveWhere(inventoryItem: iprog2,person: student4,rentalDays: 1)
		LoanHistory.findOrSaveWhere(inventoryItem: iads,person: student1,rentalDays: 3)
		
	}
	def destroy = {
	}
}
